//pages/index/index.js
const APIKEY = "c3d68814c39e43f3bd80a19483eb59b3";
var QQMapWX = require('./qqmap-wx-jssdk.js');
//var QQMapWX2 = require('./qqmap-wx-jssdk.min.js');

var qqmapsdk = new QQMapWX({
  key: '4VHBZ-STAKX-3DW4G-T7FAD-DI6FT-CLBZ2'
});
Page({
  /**
   * 页面的初始数据
   */
  data: {
       region:'loading...',
       locationId:' ',
       now:'0',
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    wx.authorize({
      scope: 'scope.userLocation',//发起定位授权
      success: function () {
        //console.log('有定位授权')
      //授权成功，此处调用获取定位函数
      wx.getLocation({//获取地址
        type: 'gcj02',
        success(res) {
          const latitude = res.latitude
          const longitude = res.longitude
          //console.log(latitude, longitude)
  
          qqmapsdk.reverseGeocoder({//SDK调用
            location: {latitude,longitude},
            success:function(res) {
              var district = res.result.address_component.district.replace('区','')
              //console.log(res)
              //console.log(district)
             that.setData({
               region:res.result.address_component.province+' ,'+res.result.address_component.city+' ,'+res.result.address_component.district
             })
  
              wx.request({
                url: 'https://geoapi.qweather.com/v2/city/lookup?location='+district+'&key=c54be298f8cf40708a4d52c4edecc8b2',
                header:{
                  'content-type':'application/json'
                },
                success(res){
                  //console.log(res)
                  that.setData({locationId:res.data.location[0].id})
                  //console.log(that.data.locationID)
  
                  wx.request({
                    url: 'https://devapi.qweather.com/v7/weather/now?location='+res.data.location[0].id+'&key=e49249ed176e47fe9e127b9845e3bc06',
                    header:{
                      'content-type':'application/json'
                    },
                    success(res){
                      //console.log(res),
                      res.data.now.obsTime=res.data.now.obsTime.replace('+08:00','   ').replace('T',' '),
                      that.setData({
                        now:res.data.now,
                      })
                    }
                  })
                }
              })
            }
          })
        }
      })
      }, fail() {
        //如果用户拒绝授权，则要告诉用户不授权就不能使用，引导用户前往设置页面。
        console.log('没有定位授权')
        wx.showModal({
          cancelColor: 'cancelColor',
          title: '没有授权无法获取位置信息',
          content: '是否前往设置页面手动开启',
          success: function (res) {
            if (res.confirm) {
              wx.openSetting({
                withSubscriptions: true,
              })
            } else {
              wx.showToast({
                icon: 'none',
                title: '您取消了定位授权，请刷新重试',
              })
            }
            
          }, fail: function (e) {
            console.log(e)
          }
        })
        wx.showToast({
          duration:20000,
          icon:'loading',
          title: '请重新进入程序'
        })
      }
    })
 
},
  
getLocation:function(){
  var that=this;
  wx.request({
    url: 'https://geoapi.qweather.com/v2/city/lookup?',
    data:{
      location:that.data.region[2],
      adm:that.data.region[1],
      key:'c3d68814c39e43f3bd80a19483eb59b3'
    },
    success:function(result){
      //console.log(result.data)
      var res=result.data
      var data = res.location[0]
      that.setData({locationId:data.id})
     
      wx.request({
       url: 'https://devapi.qweather.com/v7/weather/now?location=' +that.data.locationId+ "&key=" +APIKEY,
       success:function(result){
         var res = result.data
         //console.log(res)
         res.now.obsTime=result.data.now.obsTime.replace('+08:00','   ').replace('T',' '),
         that.setData({now:res.now})
       }
     })
    }
  })
 },
 

changeRegion:function(e){
  this.setData({
    region:e.detail.value
  })
  this.getLocation();
},





  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})